package com.itheima.demo06;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/15 9:42
 */
public class MyRunnable02 implements Runnable {
    private String pathName;
    private CountDownLatch cdl;

    public MyRunnable02(String pathName, CountDownLatch cdl) {
        this.pathName = pathName;
        this.cdl = cdl;
    }

    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            URL url = new URL(pathName);
            URLConnection conn = url.openConnection();
            InputStream in = conn.getInputStream();
            //创建字符输入流;
            bis = new BufferedInputStream(in);
            File file = new File(pathName);
            bos = new BufferedOutputStream(new FileOutputStream("E:\\cccc\\" + file.getName()));
            //读写数据 ;
            byte[] arr = new byte[1024 * 8];
            int len;
            while ((len = bis.read(arr)) != -1) {
                bos.write(arr, 0, len);
                bos.flush();
            }
            cdl.countDown();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
